package com.example.administrator.demob;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements InOnDelListener{
    private ListView lvStu;
    private Button btnAdd;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private StuAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvStu = findViewById(R.id.lv_stu);
        btnAdd = findViewById(R.id.btn_add);


        initStu();

        initAdd();

        adapter = new StuAdapter(MainActivity.this, studentArrayList,MainActivity.this);
        lvStu.setAdapter(adapter);

        initLvStu();

        readFromDB();
    }

    //从数据库中读取数据
    private void readFromDB() {
        Uri uri = Uri.parse("content://com.hjn.xn/student");

        //每次查询时，先清空
        studentArrayList.clear();

        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if(cursor != null && cursor.getCount() > 0){
            for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()){
                String no = cursor.getString(0);
                String name = cursor.getString(1);
                int age = cursor.getInt(2);
                String sex = cursor.getString(3);

                Student s = new Student(no, name, age, sex);
                studentArrayList.add(s);
            }
            cursor.close();
        }

        adapter.notifyDataSetChanged();
    }

    //编辑学生
    private void initLvStu() {
        lvStu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, EditActivity.class);
                Student student = studentArrayList.get(i);
                intent.putExtra("Student", student);
                startActivityForResult(intent, 1002);
            }
        });
    }

    //添加学生
    private void initAdd() {
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this, AddActivity.class);
                startActivityForResult(intent, 1001);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if(resultCode == 2001){
            Student student = (Student) data.getSerializableExtra("STU");
            addStuToDB(student);
        }else if(resultCode == 3001){
            Student student = (Student) data.getSerializableExtra("STU");
            updateStuToDB(student);
        }
    }

    //在数据库中修改学生
    private void updateStuToDB(Student student) {
        Uri uri = Uri.parse("content://com.hjn.xn/student");
        ContentValues values = new ContentValues();
        values.put("stuno", student.getStuNo());
        values.put("name", student.getName());
        values.put("age", student.getAge());
        values.put("sex", student.getSex());
        String where = "stuno = '"+student.getStuNo()+"'";
        getContentResolver().update(uri, values, where, null);
        readFromDB();
    }

    //将学生添加到数据库中
    private void addStuToDB(Student student) {
        Uri uri = Uri.parse("content://com.hjn.xn/student");
        ContentValues values = new ContentValues();
        values.put("stuno", student.getStuNo());
        values.put("name", student.getName());
        values.put("age", student.getAge());
        values.put("sex", student.getSex());
        getContentResolver().insert(uri, values);

        readFromDB();
    }

    //初始化学生
    private void initStu() {
        studentArrayList.add(new Student("17001","小一",17,"女"));
        studentArrayList.add(new Student("17002","小二",18,"男"));
        studentArrayList.add(new Student("17003","小三",22,"女"));
        studentArrayList.add(new Student("17004","小四",33,"女"));
        studentArrayList.add(new Student("17005","小五",27,"男"));
    }

    //删除学生
    @Override
    public void del(Student student) {
        Uri uri = Uri.parse("content://com.hjn.xn/student");
        String where = "stuno = '"+student.getStuNo()+"'";
        getContentResolver().delete(uri, where, null);
        readFromDB();
    }
}
